from commons.entity.po.travel import Travel
from commons.entity.po.user import User

from commons.entity.po.model import db


class Approval(db.Model):
    __tablename__ = 'approval'

    approval_id = db.Column(db.Integer, primary_key=True,
                            autoincrement=True, info='状态编号')
    travel_approval = db.Column(db.Integer, info='审批出差申请状态，0：未处理，1：批准，2：拒绝')
    advance_approval = db.Column(
        db.Integer, info='预支款申请状态，0：未处理，1：批准，2：拒绝，3：已处理，4：无预支款申请')
    reimbursement_approval_ma = db.Column(
        db.Integer, info='经理审批报销申请状态，0：未处理，1：批准，2：拒绝')
    reimbursement_approval_fi = db.Column(
        db.Integer, info='财务人员审批报销申请状态，0：未处理，1：批准，2：拒绝，3：已处理')
    travel_rejection = db.Column(db.String(255), info='拒绝出差申请原因')
    advance_rejection = db.Column(
        db.String(255, 'utf8mb4_0900_ai_ci'), info='拒绝预支款申请原因')
    reimbursement_rejection_ma = db.Column(
        db.String(255, 'utf8mb4_0900_ai_ci'), info='经理拒绝报销申请原因')
    reimbursement_rejection_fi = db.Column(
        db.String(255, 'utf8mb4_0900_ai_ci'), info='财务人员拒绝报销申请原因')
    travel_id = db.Column(db.ForeignKey('travel.travel_id'),
                          nullable=False, index=True, info='出差编号')
    user_id = db.Column(db.ForeignKey('user.user_id'),
                        nullable=False, index=True, info='员工编号')

    travel = db.relationship(
        'Travel', primaryjoin='Approval.travel_id == Travel.travel_id', backref='approvals')
    user = db.relationship(
        'User', primaryjoin='Approval.user_id == User.user_id', backref='approvals')

    def __str__(self) -> str:
        return (f"状态编号: {self.approval_id if self.approval_id is not None else '空'}，"
                f"审批出差申请状态，0：未处理，1：批准，2：拒绝: {self.travel_approval if self.travel_approval is not None else '空'}，"
                f"预支款申请状态，0：未处理，1：批准，2：拒绝，3：无预支款申请: {self.advance_approval if self.advance_approval is not None else '空'}，"
                f"经理审批报销申请状态，0：未处理，1：批准，2：拒绝: {self.reimbursement_approval_ma if self.reimbursement_approval_ma is not None else '空'}，"
                f"财务人员审批报销申请状态，0：未处理，1：批准，2：拒绝: {self.reimbursement_approval_fi if self.reimbursement_approval_fi is not None else '空'}，"
                f"拒绝出差申请原因: {self.travel_rejection if self.travel_rejection is not None else '空'}，"
                f"拒绝预支款申请原因: {self.advance_rejection if self.advance_rejection is not None else '空'}，"
                f"经理拒绝报销申请原因: {self.reimbursement_rejection_ma if self.reimbursement_rejection_ma is not None else '空'}，"
                f"财务人员拒绝报销申请原因: {self.reimbursement_rejection_fi if self.reimbursement_rejection_fi is not None else '空'}，"
                f"出差编号: {self.travel_id if self.travel_id is not None else '空'}，"
                f"员工编号: {self.user_id if self.user_id is not None else '空'}")

    def to_dict(self):
        return {
            'approval_id': self.approval_id,
            'travel_approval': self.travel_approval,
            'advance_approval': self.advance_approval,
            'reimbursement_approval_ma': self.reimbursement_approval_ma,
            'reimbursement_approval_fi': self.reimbursement_approval_fi,
            'travel_rejection': self.travel_rejection,
            'advance_rejection': self.advance_rejection,
            'reimbursement_rejection_ma': self.reimbursement_rejection_ma,
            'reimbursement_rejection_fi': self.reimbursement_rejection_fi,
            'travel_id': self.travel_id,
            'user_id': self.user_id
        }

    def to_dict_2(self):
        return {
            'approvalId': self.approval_id,
            'travelApproval': self.travel_approval,
            'advanceApproval': self.advance_approval,
            'reimbursementApprovalMa': self.reimbursement_approval_ma,
            'reimbursementApprovalFi': self.reimbursement_approval_fi,
            'travelRejection': self.travel_rejection,
            'advanceRejection': self.advance_rejection,
            'reimbursementRejectionMa': self.reimbursement_rejection_ma,
            'reimbursementRejectionFi': self.reimbursement_rejection_fi,
            'travelId': self.travel_id,
            'userId': self.user_id
        }
